package com.jf3q.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.jf3q.common.domain.EduChapter;
import com.jf3q.common.domain.EduVideo;
import com.jf3q.common.service.EduChapterService;
import com.jf3q.common.service.EduVideoService;
import com.jf3q.common.utils.VodUtils;
import com.jf3q.common.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/chapter")
@Tag(name = "大纲模块")
public class ChapterController {



    @Autowired
    EduChapterService chapterService;

    @Autowired
    EduVideoService videoService;

    @Autowired
    VodUtils vodUtils;

    @Operation(summary = "章节与小节展示接口")
    @PostMapping("/getBycId/{cId}")
    public ResultVo getBycId(@PathVariable String cId){
        LambdaQueryWrapper<EduChapter> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(EduChapter::getCourseId,cId);
        queryWrapper1.orderByAsc(EduChapter::getSort);
        List<EduChapter> chapterList = chapterService.list(queryWrapper1);
        for (EduChapter chapter : chapterList) {
            LambdaQueryWrapper<EduVideo> queryWrapper2=new LambdaQueryWrapper<>();
            queryWrapper2.eq(EduVideo::getChapterId,chapter.getId());
            queryWrapper2.orderByAsc(EduVideo::getSort);
            List<EduVideo> videoList = videoService.list(queryWrapper2);
            chapter.setVideos(videoList);
        }
        return ResultVo.success("",chapterList);
    }

    @Operation(summary = "添加/修改章节")
    @PostMapping("/saveUpdate")
    public ResultVo saveUpdate(@RequestBody EduChapter chapter){
        try {
            chapterService.saveOrUpdate(chapter);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("",null);
    }

    @Operation(summary = "查看章节信息")
    @PostMapping("/getOne/{chapterId}")
    public ResultVo getOne(@PathVariable String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return  ResultVo.success("",chapter);
    }

    @Operation(summary = "删除章节")
    @PostMapping("/delChapter/{chapterId}")
    @Transactional
    public ResultVo delChapter(@PathVariable String chapterId){
        LambdaQueryWrapper<EduVideo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getChapterId,chapterId);
        List<EduVideo> videoList = videoService.list(queryWrapper);
        for (EduVideo video : videoList) {
            //从阿里云删除文件
            if(StringUtils.hasText(video.getVideoSourceId().trim())){
                vodUtils.del(video.getVideoSourceId());
            }
            videoService.removeById(video.getId());
        }
        chapterService.removeById(chapterId);
        return ResultVo.success("",null);
    }


    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/delRedisCache/{courseId}")
    public ResultVo delRedisCache(@PathVariable String courseId){

        //把redis缓存更新一下
        String redisCourse = stringRedisTemplate.opsForValue().get("chapterVideoByCourseId::" + courseId);
        if (StringUtils.hasText(redisCourse)) {
            stringRedisTemplate.delete("chapterVideoByCourseId::" + courseId);
        }
        return ResultVo.success();
    }
}
